<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

//-----------------------------------------------------------------
//  Filter Acces Rightes
//-----------------------------------------------------------------

if(isset($is_guest) || isset($is_user) || isset($is_moderator))
{ header("location: $PHP_SELF?i=2"); exit; }

switch($a)
{
	//-----------------------------------------------------------------
	//  Add Link
	//-----------------------------------------------------------------

	case 1:

		//-----------------------------------------------------------------
		//  Begin Check
		//-----------------------------------------------------------------

		if(!$name){ header("location: $PHP_SELF?n=modules/main-nav&i=88"); exit; }
		if(!$link){ header("location: $PHP_SELF?n=modules/main-nav&i=89"); exit; }
		if($target == 1) $target = "blank"; else $target = "self";

		//-----------------------------------------------------------------
		//  Make sure it adds +1 in order after adding
		//-----------------------------------------------------------------

		$links_db = $db->getTable("SELECT * FROM navigation ORDER by 'order' ASC");

		foreach($links_db as $idx=>$link_id)
			$last_link_order = $link_id['order'];

		$link_order = $last_link_order + 1;

		//-----------------------------------------------------------------
		//  Add the Link
		//-----------------------------------------------------------------

		$db->insertRow("navigation",array('name' => ''.$name.'','link' => ''.$link.'','target' => ''.$target.'','order' => ''.$link_order.''));

		//-----------------------------------------------------------------
		//  Write Log 
		//-----------------------------------------------------------------

		$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc3'].' <b>navigation</b> the link -> \''.$name.'\'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/main-nav&i=85");

	break;

	//-----------------------------------------------------------------
	//  Delete Links
	//-----------------------------------------------------------------

	case 2:

		//-----------------------------------------------------------------
		//  Write Log
		//-----------------------------------------------------------------

		$checker = $db->getLine("SELECT * FROM navigation WHERE id = '$d'");
		$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc'].' <b>navigation</b> the link -> \''.$checker['name'].'\'','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Delete the Link
		//-----------------------------------------------------------------

		$db->deleteRow("navigation","id = '$d'");

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/main-nav&i=86");

	break;

	//-----------------------------------------------------------------
	//  Update Links
	//-----------------------------------------------------------------

	case 3:

		//-----------------------------------------------------------------
		//  Begin Check
		//-----------------------------------------------------------------

		foreach($name as $idx=>$val) if(!$name[$idx]){ header("location: $PHP_SELF?n=modules/main-nav&i=88"); exit; }
		foreach($link as $idx=>$val) if(!$link[$idx]){ header("location: $PHP_SELF?n=modules/main-nav&i=89"); exit; }

		//-----------------------------------------------------------------
		//  Save the Links
		//-----------------------------------------------------------------

		foreach($order as $idx=>$val)
			$db->query("Update `navigation` set `order` = '$val' WHERE `id` = '$idx'");

		foreach($name as $idx=>$val)
			$db->query("Update `navigation` set `name` = '$val' WHERE `id` = '$idx'");

		foreach($link as $idx=>$val)
		{
			$db->query("Update `navigation` set `link` = '$val' WHERE `id` = '$idx'");

			if(!$target[$idx]) $db->query("Update `navigation` set `target` = 'self' WHERE `id` = '$idx'");
		}

		foreach($target as $idx=>$val)
			$db->query("Update `navigation` set `target` = 'blank' WHERE `id` = '$idx'");

		//-----------------------------------------------------------------
		//  Write Log
		//-----------------------------------------------------------------

		$db->insertRow("log",array('user_id' => ''.$_SESSION['id'].'','user_user' => ''.$_SESSION['username'].'','log' => ''.$language['Logs desc2'].' <b>navigation</b>','date' => ''.time().''));

		//-----------------------------------------------------------------
		//  Redirect when finished
		//-----------------------------------------------------------------

		header("location: $PHP_SELF?n=modules/main-nav&i=87");

	break;

	//-----------------------------------------------------------------
	//  View Links Form
	//-----------------------------------------------------------------

	default:

		$links_db = $db->getTable("SELECT * FROM navigation ORDER by 'order' ASC");

		if($links_db != FALSE)
		{
			foreach($links_db as $idx=>$link)
			{
				if($link['target'] == "blank") $target_ab = "checked";
				else $target_ab = NULL;

				//-----------------------------------------------------------------
				//  Make Layout
				//-----------------------------------------------------------------

				$layout .= "
					<tr class='".row2color($idx)."' onMouseOver=this.className='con3' onMouseOut=this.className='".row2color($idx)."'>
					<td width='5%'><input type='text' name='order[{$link['id']}]' value='{$link['order']}' maxlength='3' class='box' style='width:100%'></td>
					<td width='40%'><input type='text' name='name[{$link['id']}]' value='{$link['name']}' maxlength='50' class='box' style='width:100%'></td>
					<td width='45%'><input type='text' name='link[{$link['id']}]' value='{$link['link']}' maxlength='200' class='box' style='width:100%'></td>
					<td width='5%'><input type='checkbox' name='target[{$link['id']}]' value='{$link['target']}' $target_ab></td>
					<td width='5%'><a href='$PHP_SELF?n=modules/main-nav&a=2&d={$link['id']}'><img src='templates/$template/images/buttons/delete.gif' border='0' alt='{$language['Menu form desc5']}'></a></td>
					</tr>";
			}
		}
		else
		{
			//-----------------------------------------------------------------
			//  Make Layout
			//-----------------------------------------------------------------

			$layout .= "
				<tr class='con1'>
				<td colspan='4'>{$language['Menu form desc8']}</td>
				</tr>\n";
		}

		//-----------------------------------------------------------------
		// Echo the back link
		//-----------------------------------------------------------------

		echo "<table class='main' cellspacing='1' cellpadding='4'><tr class='head'><td colspan='3'>{$language['Admin name']}</td></tr><tr><td colspan='3' class='con1' onMouseOver=this.className='con3' onMouseOut=this.className='con1' onclick=window.location.href='$PHP_SELF?n=modules/admin'><a href='$PHP_SELF?n=modules/admin'> &raquo; {$language['Admin back']}</a></td></tr></table><br>" ?>
			<form method="post" action="<?= $PHP_SELF ?>?n=modules/main-nav">
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td colspan="4"><?= $language['Menu add title'] ?></td></tr>
			<tr class="bottom"><td width="45%"><?= $language['Menu add desc'] ?></td><td width="42%"><?= $language['Menu add desc2'] ?></td><td width="3%"><?= $language['Menu add desc3'] ?></td><td width="10%"><?= $language['Menu add desc4'] ?></td></tr>
			<tr class="con1"><td><input type='text' name='name' class='box' style='width:100%'></td><td><input type='text' name='link' class='box' style='width:100%'></td><td><input type="checkbox" name="target" value="1"></td><td><input type='submit' name='submit' style='width:100%' class='box' value='<?= $language['Menu add desc5'] ?>'></td></tr>
			<input type="hidden" name="a" value="1">
			</table></form>
			<br>
			<form method="post" action="<?= $PHP_SELF ?>?n=modules/main-nav">
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td colspan="5"><?= $language['Menu form title'] ?></td></tr>
			<tr class="bottom"><td><?= $language['Menu form desc'] ?></td><td><?= $language['Menu form desc2'] ?></td><td><?= $language['Menu form desc3'] ?></td><td colspan="2"><?= $language['Menu form desc4'] ?></td></tr>
			<?= $layout ?>
			</table>
			<br>
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class='con1'>
			<td width="50%"><?= $language['Menu form desc6'] ?></td>
			<td width="50%" align="right"><input type='submit' name='submit' style='width:50%' class='box' value='<?= $language['Menu form desc7'] ?>'></td>
			</tr>
			<input type="hidden" name="a" value="3">
			</table></form>
		<?

	break;
}
?>